<template>
  <div class="home">
    <!-- 英雄区 -->
    <div class="hero">
      <h1>欢迎来到Vue商城</h1>
      <p>发现最优质的产品，享受最佳的购物体验</p>
      <router-link to="/products" class="btn">立即购物</router-link>
    </div>

    <!-- 热门产品 -->
    <div class="hot-products">
      <h2>热门产品</h2>
      <div class="products-grid">
        <div class="product-card" v-for="product in products" :key="product.id" @click="goToDetail(product.id)">
          <img :src="product.image" :alt="product.name" class="product-image" />
          <h3>{{ product.name }}</h3>
          <p>¥{{ product.price }}</p>
          <button class="view-btn">查看详情</button>
        </div>
      </div>
    </div>

    
    <div class="why-choose-us">
      <h2>为什么选择我们</h2>
      <div class="reasons-grid">
        <div class="reason-card">
          <img src="https://picsum.photos/60/60?random=20" alt="精选商品" class="reason-icon" />
          <h3>精选商品</h3>
          <p>我们精心挑选每一件商品，确保品质优良</p>
        </div>
        <div class="reason-card">
          <img src="https://picsum.photos/60/60?random=21" alt="快速配送" class="reason-icon" />
          <h3>快速配送</h3>
          <p>24小时内发货，全国范围内快速送达</p>
        </div>
        <div class="reason-card">
          <img src="https://picsum.photos/60/60?random=22" alt="品质保证" class="reason-icon" />
          <h3>品质保证</h3>
          <p>100%正品保证，不满意可退换</p>
        </div>
        <div class="reason-card">
          <img src="https://picsum.photos/60/60?random=23" alt="贴心服务" class="reason-icon" />
          <h3>贴心服务</h3>
          <p>24/7客户服务，随时为您解答疑问</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 模拟产品数据（含id，用于路由传参）
const products = ref([
  { id: 1, name: '智能手机', price: 2999, image: 'https://picsum.photos/300/200?random=10' },
  { id: 2, name: '无线耳机', price: 599, image: 'https://picsum.photos/300/200?random=11' },
  { id: 3, name: '智能手表', price: 1299, image: 'https://picsum.photos/300/200?random=12' },
  { id: 4, name: '笔记本电脑', price: 5999, image: 'https://picsum.photos/300/200?random=13' }
])

// 点击跳转商品详情页
const goToDetail = (id) => {
  router.push({ name: 'ProductDetail', params: { id } })
}
</script>

<style scoped>
/* 全局容器样式 */
.home {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40px 20px;
}

/* 英雄区样式 */
.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  width: 100%;
  height: 400px;
  background: linear-gradient(135deg, #4a6fa5, #6a4a9e);
  color: white;
  border-radius: 8px;
}
.hero h1 {
  font-size: 36px;
  margin-bottom: 16px;
}
.hero p {
  font-size: 18px;
  margin-bottom: 24px;
}
.btn {
  background: white;
  color: #4a6fa5;
  padding: 12px 24px;
  border-radius: 30px;
  text-decoration: none;
  font-weight: bold;
  transition: transform 0.3s;
}
.btn:hover {
  transform: translateY(-3px);
}

/* 热门产品样式 */
.hot-products {
  width: 100%;
  margin-top: 60px;
}
.hot-products h2 {
  text-align: center;
  font-size: 28px;
  margin-bottom: 32px;
  position: relative;
  display: inline-block;
  left: 50%;
  transform: translateX(-50%);
}
.hot-products h2::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 3px;
  background: #6a4a9e;
  border-radius: 3px;
}
.products-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
}
.product-card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s, box-shadow 0.3s;
  text-align: center;
  cursor: pointer;
}
.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}
.product-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}
.product-card h3 {
  margin: 16px 0 8px;
  font-size: 18px;
}
.product-card p {
  color: #6a4a9e;
  font-weight: bold;
  margin-bottom: 16px;
}
.view-btn {
  background: #6a4a9e;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  margin-bottom: 16px;
  transition: background 0.3s;
}
.view-btn:hover {
  background: #4a6fa5;
}

/* 为什么选择我们样式 */
.why-choose-us {
  width: 100%;
  margin-top: 60px;
}
.why-choose-us h2 {
  text-align: center;
  font-size: 28px;
  margin-bottom: 32px;
  position: relative;
  display: inline-block;
  left: 50%;
  transform: translateX(-50%);
}
.why-choose-us h2::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 3px;
  background: #6a4a9e;
  border-radius: 3px;
}
.reasons-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 24px;
}
.reason-card {
  background: white;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
  text-align: center;
}
.reason-icon {
  width: 60px;
  height: 60px;
  margin-bottom: 16px;
  object-fit: contain;
}
.reason-card h3 {
  margin-bottom: 8px;
  font-size: 18px;
}
.reason-card p {
  color: #666;
  font-size: 14px;
}
</style>